package com.example.service.impl.ums;

import com.alibaba.fastjson.JSONObject;
import com.example.common.UUIDUtils;
import com.example.mapper.ums.RoleMenuMapper;
import com.example.mapper.ums.UmsMenuMapper;
import com.example.mapper.ums.UmsRoleMapper;
import com.example.model.ums.RoleMenu;
import com.example.model.ums.UmsMenu;
import com.example.model.ums.UmsRole;
import com.example.model.vo.UmsMenuVo;
import com.example.service.ums.UmsMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单Service
 */
@Service
@Transactional
public class UmsMenuServiceImpl implements UmsMenuService {

    @Autowired
    private UmsMenuMapper mapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UmsRoleMapper umsRoleMapper;



    @Override
    public PageInfo<UmsMenu> findUmsMenusTreePage(UmsMenuVo vo) {
        PageHelper.startPage(vo.getPage(),vo.getRows());//分页起始码以及每页页数

        //返回的集合
        List<UmsMenu> restList = new ArrayList<>();
        UmsMenu umsMenu1 = new UmsMenu();
        umsMenu1.setParentId("-1");
        //查询出所有菜单
        List<UmsMenu> umsMenusByParam = mapper.findUmsMenusByParam(umsMenu1);
        //循环菜单改成树形结构
        for (UmsMenu item: umsMenusByParam) {
            //等于null说明是一级菜单
            if(item.getParentId() == null){
                //找到所有父级菜单是当前菜单的
                UmsMenu umsMenu = new UmsMenu();
                umsMenu.setParentId(item.getId());
                List<UmsMenu> umsMenusChildList = mapper.findUmsMenusByParam(umsMenu);
                item.setMenus(umsMenusChildList);
                restList.add(item);
            }
        }
        PageInfo pageInfo=new PageInfo(umsMenusByParam);
        return pageInfo;
    }

    @Override
    public JSONObject findUmsMenusTree(UmsMenuVo vo) throws Exception {
        //返回json
        JSONObject json = new JSONObject();
        List<UmsMenu> umsMenuByRoleId = mapper.findUmsMenuByRoleId(vo);
        //获取到集合中的id
        List<String> collect = umsMenuByRoleId.stream().filter(Objects::nonNull)
                .map(item -> item.getId())
                .collect(Collectors.toList());
        json.put("collect",collect);
        List<UmsMenu> restList = new ArrayList<>();
        UmsMenu umsMenu1 = new UmsMenu();
        umsMenu1.setParentId("-1");
        //查询出所有菜单
        List<UmsMenu> umsMenusByParam = mapper.findUmsMenusByParam(umsMenu1);
        //循环菜单改成树形结构
        for (UmsMenu item: umsMenusByParam) {
            //等于null说明是一级菜单
            if(item.getParentId() == null){
                //找到所有父级菜单是当前菜单的
                UmsMenu umsMenu = new UmsMenu();
                umsMenu.setParentId(item.getId());
                List<UmsMenu> umsMenusChildList = mapper.findUmsMenusByParam(umsMenu);
                item.setMenus(umsMenusChildList);
                restList.add(item);
            }
        }
        json.put("umsMenus",umsMenusByParam);
        return json;
    }

    @Override
    public List<UmsMenu> findUmsMenusByParam(UmsMenu roleMenu) {
        return mapper.findUmsMenusByParam(roleMenu);
    }

    @Override
    public UmsMenu findUmsMenuByParam(UmsMenu roleMenu) {
        return mapper.findUmsMenuByParam(roleMenu);
    }

    @Override
    public List<UmsMenu> findUmsMenuByAdminId(UmsMenuVo umsMenu) {
        return mapper.findUmsMenuByAdminId(umsMenu);
    }

    @Override
    public int insert(UmsMenu menu) {
        menu.setId(UUIDUtils.getId());
        menu.setCreateTime(new Date());
        int insert = mapper.insert(menu);
        if(insert == 1){
            UmsRole umsRole = new UmsRole();
            umsRole.setRoleCode("superAdministrator");
            UmsRole umsRoleByParam = umsRoleMapper.findUmsRoleByParam(umsRole);
            if(null == umsRoleByParam){
                return 0;
            }
            //为用户分配管理员角色
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setId(UUIDUtils.getId());
            roleMenu.setRoleId(umsRoleByParam.getId());
            roleMenu.setMenuId(menu.getId());
            roleMenuMapper.insert(roleMenu);
        }
        return insert;
    }

    @Override
    public int updateMenuByParam(UmsMenu roleMenu) {
        return mapper.updateMenuByParam(roleMenu);
    }

    @Transactional
    @Override
    public void deleteById(String id) throws Exception {
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setParentId(id);
        List<UmsMenu> umsMenusByParam = mapper.findUmsMenusByParam(umsMenu);
        if(umsMenusByParam.size() != 0){
            for (UmsMenu item: umsMenusByParam) {
                int i = mapper.deleteById(item.getId());
                if(i != 1){
                    throw new Exception("删除失败!");
                }
                delRoleMenu(item.getId());
            }
        }
        int deleteById = mapper.deleteById(id);
        if(deleteById == 1){
            delRoleMenu(id);
        }else{
            throw new Exception("删除失败!");
        }
    }

    public void delRoleMenu(String id){
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setMenuId(id);
        List<RoleMenu> roleMenuByParam = roleMenuMapper.findRoleMenuByParam(roleMenu);
        if(roleMenuByParam.size() != 0){
            for (RoleMenu item:roleMenuByParam) {
                roleMenuMapper.deleteById(item.getId());
            }
        }
    }
}
